package com.scrum.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.scrum.Object.Comment;
import com.scrum.Object.User;

/**
 * Servlet implementation class Add_Comment
 */
@WebServlet("/add_comment")
public class Add_Comment extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public Add_Comment() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String comment = request.getParameter("comment");
		System.out.println(request.getParameter("postid"));
		int post_id = Integer.parseInt(request.getParameter("postid"));
		HttpSession session = request.getSession();
		int currentuser_id;
		try {
			currentuser_id = ((User) session.getAttribute("currentuser"))
					.getId();
		} catch (Exception ex) {
			currentuser_id = 1;
		}
		
		try {
			Comment.addComment(currentuser_id, post_id, comment);
		} catch (ClassNotFoundException | SQLException e) {
			e.printStackTrace();
		}
		response.setStatus(200);
		response.setContentType("text/plain");
		response.setCharacterEncoding("UTF-8");
		response.getWriter().write("First domo");
	}

}
